// 加载新闻列表
async function loadNewsList() {
    try {
        const response = await HttpClient.post(CONFIG.API.MESSAGE.SEARCH, {
            mtype: 2  // 新闻类型
        });

        const newsListElement = document.getElementById('newsList');
        
        if (Array.isArray(response)) {
            response.forEach(news => {
                const newsCard = document.createElement('div');
                newsCard.className = 'news-card';
                newsCard.onclick = () => window.location.href = `./detail.html?mid=${news.mid}`;
                
                newsCard.innerHTML = `
                    <h2>${news.head}</h2>
                    <div class="news-meta">
                        <div class="date-container">
                            <i class="far fa-calendar-alt"></i>
                            <span>${new Date(news.mtime).toLocaleDateString('zh-CN')}</span>
                        </div>
                    </div>
                    <div class="news-preview">
                        ${news.context.substring(0, 200)}...
                    </div>
                `;
                
                newsListElement.appendChild(newsCard);
            });
        }
    } catch (error) {
        console.error('加载新闻列表失败:', error);
    }
}

// 退出登录
function handleLogout() {
    localStorage.removeItem('userInfo');
    window.location.href = '../login.html';
}

// 更新用户信息显示
function updateUserInfo() {
    const userInfo = JSON.parse(localStorage.getItem('userInfo'));
    const userInfoElement = document.getElementById('userInfo');
    
    if (userInfo && userInfo.uid) {
        // 用户已登录
        const defaultAvatar = "../../images/default-avatar.svg";
        
        userInfoElement.innerHTML = `
            <div class="user-avatar-container">
                <div class="user-avatar">
                    <img src="${defaultAvatar}" alt="用户头像">
                </div>
                <div class="dropdown-menu">
                    <a href="profile.html">个人中心</a>
                    <div class="divider"></div>
                    <a href="javascript:void(0)" onclick="handleLogout()">退出登录</a>
                </div>
            </div>
        `;

        // 添加头像点击事件，改为点击切换显示/隐藏
        const avatarContainer = userInfoElement.querySelector('.user-avatar-container');
        const dropdownMenu = userInfoElement.querySelector('.dropdown-menu');
        
        // 点击头像时切换下拉菜单的显示状态
        avatarContainer.querySelector('.user-avatar').addEventListener('click', (e) => {
            e.stopPropagation(); // 阻止事件冒泡
            dropdownMenu.classList.toggle('show');
        });

        // 点击页面其他地方时关闭下拉菜单
        document.addEventListener('click', () => {
            dropdownMenu.classList.remove('show');
        });

        // 点击下拉菜单内容时阻止事件冒泡，这样点击菜单项时不会触发document的点击事件
        dropdownMenu.addEventListener('click', (e) => {
            e.stopPropagation();
        });
    } else {
        // 用户未登录
        userInfoElement.innerHTML = `
            <a href="../login.html">登录</a>
            <span>/</span>
            <a href="../register.html">注册</a>
        `;
    }
}

// 页面加载完成后执行初始化
document.addEventListener('DOMContentLoaded', () => {
    // 检查登录状态和身份
    const userInfo = JSON.parse(localStorage.getItem('userInfo'));
    
    // 更新用户信息显示（包括头像）
    updateUserInfo();
    
    if (!userInfo) {
        // 未登录用户只能看新闻
        loadNewsList();
        return;
    }

    // 检查用户类型
    switch(userInfo.utype) {
        case 0: // 管理员
            window.location.href = '../admin/index.html';
            return;
        case 1: // 员工
            window.location.href = '../employee/index.html';
            return;
        case 2: // 普通用户
            // 继续加载用户页面
            loadNewsList(); // 只能看新闻
            break;
        default:
            window.location.href = '../login.html';
            return;
    }
}); 